<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<meta name="renderer" content="webkit">
		<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
		<meta name="viewport" content="maximum-scale=3, minimum-scale=1, initial-scale=1, width=device-width, user-scalable=no">
		<title>...</title>
		<meta name="description" content="">
		<link rel="stylesheet" href="../../css/base.css" />
		<script type="text/javascript" src="../../js/base.js" ></script>
		<script type="text/javascript" src="../../js/jquery-3.3.1.js" ></script>
		<script type="text/javascript" src="https://res.wx.qq.com/open/js/jweixin-1.6.0.js"></script>
	</head>
	<body>
		<div class="defHint">正在加载,请稍后..</div>
	</body>
	<script>
		if(getUrlParam().memKey){ // 小程序进入
			isWeixin();
			localStorage.memKey=getUrlParam().memKey;
			localStorage.mchKey=getUrlParam().mchKey;
			localStorage.orderNo=getUrlParam().orderNo;
			localStorage.currentPrice=getUrlParam().currentPrice;
			localStorage.payType=getUrlParam().payType;
			localStorage.activeType=getUrlParam().activeType;
			// 判断堂食或外卖
			if(localStorage.orderNo){
				localStorage.memTransType="TAKEOUT";
			} else {
				localStorage.memTransType="DINNING";
			}
			getMchInfo();
		} 
		
			
		// 获取商户信息
		function getMchInfo(){ 
	    	$.ajax({
				type:"post",
				url:url+"mch_info",
				data:{
					mchKey:localStorage.mchKey,
					browerType:localStorage.browerType,
					code:localStorage.code,
					wechatPayCode:localStorage.wechatPayCode,
					memKey:localStorage.memKey,
					auth_code:""
				},
				success:function(e){
					alert(JSON.stringify(e))
					if(e.code=="0000"){
						if(e.result.existsPayOpenId=="FALSE"){
							let url="https://cdadmin.landdt.cn/h5/pay/index.html?mchkey=" + localStorage.mchkey + "&orderNo=" + localStorage.orderNo + "&currentPrice=" + localStorage.currentPrice + "&payType=" + localStorage.payType + "&activeType=" + localStorage.activeType + "&memKey=" + localStorage.memKey + "#wechat_redirect";
							location.replace(url);
							return
						}
						
						if(e.result.isPullFlag=="TRUE"){
							getMemberInfo(e.result,function(e){
								localStorage.mchInfo=JSON.stringify(e.result);
								if(localStorage.orderNo){
									orderNoPay(e.result);
								} else if(localStorage.payType=="CHARGE"&&e.result.openMember=="TRUE"){
									charge(e.result);
								} else{
									location.href="pay.html";
								}
							})
						} else {
							localStorage.mchInfo=JSON.stringify(e.result);
							if(localStorage.orderNo){
								orderNoPay(e.result);
							} else if(localStorage.payType=="CHARGE"&&e.result.openMember=="TRUE"){
								charge(e.result);
							} else{
								location.href="pay.html";
							}
						}
					} else {
						hint(e.msg);
					}
				},
				error:function(e){
	    			hint(e.msg);
	    		}
			});
		};
		
		//缓存健康生活公众号code
		localStorage.code=getUrlParam().code;
		if(localStorage.code){
			getMchInfo();
		}else{
			hint("获取code失败");
		}
		
		// 更新会员信息
		function getMemberInfo(e,fun){
			if(!localStorage.code){
				let url="https://cdadmin.landdt.cn/h5/pay/index.html?mchkey=" + localStorage.mchkey + "&orderNo=" + localStorage.orderNo + "&currentPrice=" + localStorage.currentPrice + "&payType=" + localStorage.payType + "&activeType=" + localStorage.activeType + "&memKey=" + localStorage.memKey + "#wechat_redirect";
				location.replace(url);
				return
			}
			$.ajax({
				type:"post",
				url:url+"mch_info",
				data:{
					browerType:localStorage.browerType,
					code:localStorage.code,
					memKey:e.memKey,
				},
				success:function(e){
					if(e.code=="0000"){
						fun();
					} else {
						hint(e.msg);
					}
				},
				error:function(e){
	    			hint(e.msg);
	    		}
			});
		}
		
		// 小程序订单支付
		function orderNoPay(mchInfo) {
	    	let data={
	    		mchKey:localStorage.mchKey,
				currentPrice:localStorage.currentPrice,
				browerType:localStorage.browerType,
				openMember:mchInfo.openMember,
				tradeTag:mchInfo.tradeTag,
				memTransType:"TAKEOUT",
				memKey:mchInfo.memKey,
				orderNo:localStorage.orderNo
			};
	            	
	    	$.ajax({
				type:"post",
				url:url+"confirm_payment_amount",
				data:data,
				success:function(e){
					if(e.code=="0000"){
						localStorage.payInfo=JSON.stringify(e.result);
						
						if(e.result.toNext=="BALANCE_OK"){ //余额支付成功
				    		location.href="pay-result.html";
				    	} else if(e.result.toNext=="NONE"){ //不开启会员，直接支付订单
							let payInfo=JSON.parse(e.result.payParams.payInfo);
							wxPay(payInfo,function(){
							   	var url="/pages/index/navigationbar/navigationbar?iconPageThat=order&orderNo="+localStorage.orderNo;
								closePay(url,{
									iconPageThat:"order",
									orderNo:localStorage.orderNo
								});
							});
				    	} else {
				    		location.href="noPay.html";
				    	}
					} else {
						hint(e.msg);
					}
				},
				error:function(e){
	    			hint(e.msg);
	    		}
			});
	    };
		
		
		// 小程序充值
	    function charge(mchInfo){
	    	
	    	var data={
	    		mchKey:localStorage.mchKey,
	    		memKey:mchInfo.memKey,
				currentPrice:localStorage.currentPrice,
				browerType:localStorage.browerType,
				tradeTag:mchInfo.tradeTag,
				activeType: localStorage.activeType,
				payType: "CHARGE",
				toNext: "",
				memTransType: "CHARGE",
				orderNo:""
			};

	    	$.ajax({
				type:"post",
				url:url+"pay_money",
				data:data,
				success:function(e){
					if(e.code=="0000"){
						localStorage.result=JSON.stringify(e.result);
						let payInfo=JSON.parse(e.result.payParams.payInfo);
						wxPay(payInfo,function(){
							closePay();
						});
					} else {
						hint(e.msg);
					}
				},
				error:function(e){
	    			hint(e.msg);
	    		}
			});
	   	};
	   	
	</script> 
</html>